/**
 * @file request
 *
 * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Content_negotiation
 */

var accepts = require('accepts');
var util = require('util');
var http = require('http');

function Request() {

}

util.inherits(Request, http.IncomingMessage);

Request.prototype.accepts = function() {
    var accept = accepts(this);
    return accept.type.apply(accept, arguments);
};

Request.prototype.param = function(name, defaultValue) {
    var param = this.param || {};
    var body = this.body || {};
    var query = this.query || {};

    var args = arguments.length === 1 ? 'name' : 'name, default';

    if (null != param[name] && param.hasOwnProperty(name)) {
        return param[name];
    }

    if (null != body[name]) {
        return body[name];
    }

    if (null != query[name]) {
        return query[name];
    }

    return defaultValue;
};

Request.prototype.get = Request.prototype.header = function(name) {
    var lc = name.toLowerCase();
    switch (lc) {
        case 'referer':
        case 'referrer':
            return this.headers.referrer || this.headers.referer;
        default:
            return this.headers[lc];
    }
};

module.exports = new Request();
